package aryn.framework.display.drawable;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import aryn.framework.display.ButtonBorder;
import aryn.framework.display.Clickable;
import aryn.framework.display.MyCanvas;
import aryn.framework.display.MyCanvasLogic;
import aryn.framework.display.MyMouseInfo;
import aryn.framework.utilities.Util;

public class DrawingButton extends Drawable implements MyCanvasLogic, Clickable {

	public enum ButtonState {
		NORMAL(0), PRESSED(1), OVER(2), DISABLED(3);

		public int value;

		private ButtonState(int value) {
			this.value = value;
		}
	}

	private BufferedImage images[];
	private ButtonBorder borders[];

	private ButtonState buttonState = ButtonState.NORMAL;
	
	// prendere in ingresso una stringa e scriverla al centro del bottone. Se esiste un bitmap font, passarlo nel costruttore.
	public DrawingButton(BufferedImage image, int x, int y, MyCanvas canvas) {
		this(new BufferedImage[ButtonState.values().length],
				new ButtonBorder[ButtonState.values().length], x, y, canvas);
		for (int i = 0; i < ButtonState.values().length; i++) {
			images[i] = image;
			borders[i] = new ButtonBorder(x, y, image.getWidth(canvas),
					image.getHeight(canvas), canvas);
		}
	}

	public DrawingButton(BufferedImage[] images, int x, int y, MyCanvas canvas) {
		this(images, new ButtonBorder[ButtonState.values().length], x, y,
				canvas);
		int width = images[0].getWidth(canvas);
		int height = images[0].getHeight(canvas);
		setBorder(ButtonState.NORMAL, new ButtonBorder(x, y, width, height,
				canvas, false));
		setBorder(ButtonState.PRESSED, new ButtonBorder(x, y, width, height,
				canvas, true));
		setBorder(ButtonState.OVER, new ButtonBorder(x, y, width, height,
				canvas));
		setBorder(ButtonState.DISABLED, new ButtonBorder(x, y, width, height,
				canvas));
	}

	public DrawingButton(BufferedImage[] images, ButtonBorder[] borders, int x,
			int y, MyCanvas canvas) {
		super(x, y, Priority.FOREGROUND, canvas);
		this.images = images;
		this.borders = borders;
	}

	public void setImage(ButtonState type, BufferedImage image) {
		images[type.value] = image;
	}

	public void setBorder(ButtonState type, ButtonBorder border) {
		borders[type.value] = border;
	}

	public ButtonState getButtonState() {
		return buttonState;
	}

	protected void setButtonState(ButtonState buttonState) {
		this.buttonState = buttonState;
	}

	public void draw(Graphics g) {
		BufferedImage img = images[buttonState.value];
		int imageWidth = img.getWidth(canvas);
		int imageHeight = img.getHeight(canvas);
		// Insets insets = borders[buttonState].getInsets();
		// int x = insets.left + this.x;
		// int y = insets.top + this.y;

		if (x + imageWidth >= 0 && y + imageHeight >= 0
				&& x <= canvas.getWidth() && y <= canvas.getHeight())
			g.drawImage(img, x, y, canvas);

		borders[buttonState.value].draw(g);
	}

	@Override
	public boolean isMouseOver(int mouseX, int mouseY) {
		BufferedImage img = images[buttonState.value];
		return Util.mouseBound(mouseX, mouseY, this.x, this.y,
				this.x + img.getWidth(), this.y + img.getHeight());
	}

	public boolean isMouseOverIncludingBorders(int mouseX, int mouseY) {
		return borders[buttonState.value].isMouseOver(mouseX, mouseY);
	}

	@Override
	public void update(double dt) {
		if (!buttonState.equals(ButtonState.DISABLED)) {
			buttonState = ButtonState.NORMAL;
			if (isMouseOver()) {
				buttonState = ButtonState.OVER;
				if (isLeftPressed())
					buttonState = ButtonState.PRESSED;
			}
		}
	}

	@Override
	public boolean isMouseOver() {
		MyMouseInfo info = canvas.getMouseInfo();
		int mouseX = info.getMouseX();
		int mouseY = info.getMouseY();
		return isMouseOver(mouseX, mouseY);
	}

}
